package net.takela.common.spring.http;


import net.takela.common.utils.JSONUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * @author baboy
 * create at 2023/1/22
 */
public class HttpResponse<T> {

    /**
     * 返回状态码
     */
    private int code = StatusCode.SUCCESS.getCode();
    /**
     * 返回状态信息
     */
    private String message = StatusCode.SUCCESS.getMessage();
    /**
     * 返回的数据
     */
    private T data;
    /**
     * 
     */
    public HttpResponse() {

    }
    /**
     * 
     */
    public HttpResponse(StatusCode statusCode) {
        this.code = statusCode.getCode();
        this.message = statusCode.getMessage();
    }
    /**
     * 
     */
    public HttpResponse(T d) {
        this.setData(d);
    }
    /**
     * 
     */
    public HttpResponse(int status, String msg) {
        this.code = status;
        this.message = msg;
    }
    /**
     * 
     */
    public HttpResponse(int status, String msg, T data) {
        this.code = status;
        this.message = msg;
        this.data = data;
    }
    /**
     * 
     */
    public int getCode() {
        return code;
    }
    /**
     * 
     */
    public HttpResponse<T> setCode(int code) {
        this.code = code;
        return this;
    }
    /**
     * 
     */
    public String getMessage() {
        return message;
    }
    /**
     * 
     */
    public HttpResponse<T> setMessage(String message) {
        this.message = message;
        return this;
    }
    /**
     * 
     */
    public T getData() {
        return data;
    }
    /**
     * 
     */
    public HttpResponse<T> setData(T data) {
        this.data = data;
        return this;
    }

    
    /**
     * 
     */
    @JsonIgnore
    public boolean isSuccess() {
        return this.code == StatusCode.SUCCESS.getCode();
    }
    /**
     * 
     */
    public boolean hasData() {
        return data != null;
    }

    
    /**
     * 
     */
    @JsonIgnore
    public String getErrorMessage() {
        if (this.isSuccess()) {
            return null;
        }
        if (this.message == null) {
            this.message = "Server Error";
        }
        return this.message;
    }

    
    /**
     * 
     */
    @Override
    public String toString() {
        return JSONUtils.toString(this);
    }
    /**
     * 
     */
    public static <T> HttpResponse<T> paramError() {
        return new HttpResponse<>(StatusCode.PARAM_ERROR);
    }
    /**
     * 
     */
    public static <T> HttpResponse<T> paramError(String msg) {
        return new HttpResponse<T>(StatusCode.PARAM_ERROR).setMessage(msg);
    }
    /**
     * 
     */
    public static <T> HttpResponse<T> success() {
        return new HttpResponse<>(StatusCode.SUCCESS);
    }
    /**
     * 
     */
    public static <T> HttpResponse<T> success(T data) {
        HttpResponse<T> response = new HttpResponse<>(StatusCode.SUCCESS);
        response.setData(data);
        return response;
    }
    /**
     * 
     */
    public static <T> HttpResponse<T> error() {
        return new HttpResponse<>(StatusCode.ERROR);
    }
    /**
     * 
     */
    public static <T> HttpResponse<T> error(int code, String message) {
        return new HttpResponse<>(code, message);
    }
    /**
     * 
     */
    public static <T> HttpResponse<T> error(StatusCode statusCode) {
        return new HttpResponse<>(statusCode);
    }
}
